package com.daylightrobbery.server.packet.game.common;

import org.springframework.core.io.ClassPathResource;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;

public class DataReader {

    public static final String filePath = "blockData.txt";

    public static HashMap<Integer, BlockData> read(){
        ArrayList<Integer> indexes = new ArrayList<>();
        ArrayList<String> chineseNames = new ArrayList<>();
        ArrayList<String> englishNames = new ArrayList<>();
        ArrayList<Integer> moneys = new ArrayList<>();
        ArrayList<Integer> level0Prices = new ArrayList<>();
        ArrayList<Integer> level1Prices = new ArrayList<>();
        ArrayList<Integer> level2Prices = new ArrayList<>();
        ArrayList<Integer> level3Prices = new ArrayList<>();
        ArrayList<Integer> level4Prices = new ArrayList<>();
        ArrayList<Integer> level5Prices = new ArrayList<>();
        ArrayList<Integer> levelUpPrices = new ArrayList<>();
        ArrayList<Integer> mortgagePrices = new ArrayList<>();
        ArrayList<Integer> ascriptions = new ArrayList<>();
        ArrayList<Integer> blockType = new ArrayList<>();

        ClassPathResource resource = new ClassPathResource(filePath);
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            while ((line = reader.readLine()) != null) {
                // 假设每个值之间使用逗号分隔
                String[] parts = line.split(",");
                if (parts.length == 14) { // 确保数据格式正确
                    indexes.add(Integer.parseInt(parts[0]));
                    blockType.add(Integer.parseInt(parts[1]));
                    chineseNames.add(parts[2]);
                    englishNames.add(parts[3]);
                    moneys.add(Integer.parseInt(parts[4]));
                    level0Prices.add(Integer.parseInt(parts[5]));
                    level1Prices.add(Integer.parseInt(parts[6]));
                    level2Prices.add(Integer.parseInt(parts[7]));
                    level3Prices.add(Integer.parseInt(parts[8]));
                    level4Prices.add(Integer.parseInt(parts[9]));
                    level5Prices.add(Integer.parseInt(parts[10]));
                    levelUpPrices.add(Integer.parseInt(parts[11]));
                    mortgagePrices.add(Integer.parseInt(parts[12]));
                    ascriptions.add(Integer.parseInt(parts[13]));
                }else{
                    for (String part : parts) {
                        System.out.println(part);
                        throw new RuntimeException("Error!");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        HashMap<Integer, BlockData> map = new HashMap<>();

        for (int i = 0; i < indexes.size(); i++) {
            BlockData blockData = new BlockData();
            blockData.setBlockType(blockType.get(i));
            blockData.setChineseName(chineseNames.get(i));
            blockData.setEnglishName(englishNames.get(i));
            blockData.setMoney(moneys.get(i));
            blockData.setIndex(indexes.get(i));
            blockData.setLevel0Price(level0Prices.get(i));
            blockData.setLevel1Price(level1Prices.get(i));
            blockData.setLevel2Price(level2Prices.get(i));
            blockData.setLevel3Price(level3Prices.get(i));
            blockData.setLevel4Price(level4Prices.get(i));
            blockData.setLevel5Price(level5Prices.get(i));
            blockData.setLevelUpPrice(levelUpPrices.get(i));
            blockData.setMortgagePrice(mortgagePrices.get(i));
            blockData.setAscription(ascriptions.get(i));
            System.out.println(blockData);
            map.put(indexes.get(i), blockData);
        }
        return map;
    }


}
